package gold.digger;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC151 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String reverseWords(String s) {
            s = s.trim();
            if (s.equals("")) {
                return "";
            }

            String[] splited = s.split(" ");
            Stack<String> stack = new Stack<>();
            StringBuffer sb = new StringBuffer();
            for (String word : splited) {
                if (!isBlankWord(word)) stack.push(word);
            }

            while (!stack.isEmpty()) sb.append(stack.pop()).append(" ");
            sb.deleteCharAt(sb.length() - 1);
            return sb.toString();
        }

        public boolean isBlankWord(String word) {
            return null == word || word.length() <= 0 || word.equals(" ");
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.reverseWords(" "));
    }

    public static void main(String[] args) throws Exception {
        LC151 an = new LC151();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
